package qeb.handheldmedical.patient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.download.Download;
import qeb.handheldmedical.download.DownloadActivity;
import qeb.handheldmedical.download.Download.downhandler;
import qeb.handheldmedical.helper.MyAnimations;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.ExamChoose;
import qeb.handheldmedical.model.ExamItem;
import qeb.handheldmedical.model.InPatient;
import android.R.anim;
import android.R.integer;
import android.app.Activity;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.JsonWriter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年9月8日 下午9:34:13
 * @declaration 入院记录
 */
public class PatientRyjl extends Activity {
	Button btn_title_back, btn_title_right;
	// 入院记录数据
	private TextView tv_top_title;
	private JSONObject ryjlJsonObject;
	InPatient pi = new InPatient();
	private boolean areButtonsShowing;
	private RelativeLayout composerButtonsWrapper;
	private ImageView composerButtonsShowHideButtonIcon;
	private RelativeLayout composerButtonsShowHideButton;
	private ImageView composer_button_zs, composer_button_xbs,
			composer_button_hys, composer_button_grs, composer_button_jws,
			composer_button_jzs, composer_button_fzjc, composer_button_xthg,
			composer_button_yjs;
	private TextView tv_ryjltitle, tv_ryjlcontext;
	final int UPDATE = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.patient_ryjl);
		pi = (InPatient) getIntent().getSerializableExtra("patientinfo");

		MyAnimations.initOffset(PatientRyjl.this);
		initUi();
	}

	private void initUi() {
		initData();
		initComposerUi();
		tv_ryjltitle = (TextView) findViewById(R.id.tv_ryjltitle);
		tv_ryjlcontext = (TextView) findViewById(R.id.tv_ryjlcontext);
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(mClickListener);
		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setVisibility(View.INVISIBLE);
		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText(pi.XM + "——大病历");
		composer_button_zs = (ImageView) findViewById(R.id.composer_button_zs);
		composer_button_zs.setOnClickListener(mClickListener);
		composer_button_xbs = (ImageView) findViewById(R.id.composer_button_xbs);
		composer_button_xbs.setOnClickListener(mClickListener);
		composer_button_jws = (ImageView) findViewById(R.id.composer_button_jws);
		composer_button_jws.setOnClickListener(mClickListener);
		composer_button_grs = (ImageView) findViewById(R.id.composer_button_grs);
		composer_button_grs.setOnClickListener(mClickListener);
		composer_button_hys = (ImageView) findViewById(R.id.composer_button_hys);
		composer_button_hys.setOnClickListener(mClickListener);
		composer_button_jzs = (ImageView) findViewById(R.id.composer_button_jzs);
		composer_button_jzs.setOnClickListener(mClickListener);
		composer_button_fzjc = (ImageView) findViewById(R.id.composer_button_fzjc);
		composer_button_fzjc.setOnClickListener(mClickListener);
		composer_button_xthg = (ImageView) findViewById(R.id.composer_button_xthg);
		composer_button_xthg.setOnClickListener(mClickListener);
		composer_button_yjs = (ImageView) findViewById(R.id.composer_button_yjs);
		composer_button_yjs.setOnClickListener(mClickListener);
	}

	/**
	 * 加载数据
	 */
	private void initData() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					getData();
					myHandler.sendEmptyMessage(UPDATE);

				} catch (Exception ex) {

				}

			}
		}).start();
	}

	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE:
				tv_ryjltitle.setText("入院记录");
				setTextViewContext("Zs");
				break;
			default:
				break;
			}
			super.handleMessage(msg);

		}
	};

	/**
	 * 获取值
	 * 
	 * @throws JSONException
	 */
	private void getData() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("zyxh", pi.ZYXH);
		ryjlJsonObject = WSHelper.getWebserviceJsonObject("GetRyjlInfo", map);

	}

	/**
	 * 设置显示内容
	 * 
	 * @param name
	 */
	private void setTextViewContext(String name) {
		String context = "";
		try {
			if (name.equals("Zs")) {
				context = "<font color=\"#000000\">主　　诉：</font>"
						+ ryjlJsonObject.getString("Zs").replaceAll("\n",
								"<br/>　　");
				context += "<br/><font color=\"#000000\">入院诊断：</font>"
						+ ryjlJsonObject.getString("Ryzd").replaceAll("\n",
								"<br/>　　");
				context += "<br/><font color=\"#000000\">书写时间：</font>"
						+ ryjlJsonObject.getString("Sxsj").replaceAll("\n",
								"<br/>　　");
				tv_ryjlcontext.setText(Html.fromHtml(context+"<br/><br/><br/>"));
			}
			// 辅助检查
			else if (name.equals("Fzjc")) {
				context = "<font color=\"#000000\">病 理 检 查 ：</font>"
						+ ryjlJsonObject.getString("Fzjc_bljc").replaceAll(
								"\n", "<br/>　　");
				context += "<br/><font color=\"#000000\">实验室检查：</font>"
						+ ryjlJsonObject.getString("Fzjc_sysjc").replaceAll(
								"\n", "<br/>　　");
				context += "<br/><font color=\"#000000\">特 殊 检 查 ：</font>"
						+ ryjlJsonObject.getString("Fzjc_tsjc").replaceAll(
								"\n", "<br/>　　");
				tv_ryjlcontext.setText(Html.fromHtml(context+"<br/><br/><br/>"));
			} else {
				context = ryjlJsonObject.getString(name).replaceAll("\n",
						"\n　　");
				tv_ryjlcontext.setText("\u3000\u3000" + context + "\n\n\n");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			// 主诉
			case R.id.composer_button_zs:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("入院记录");
				setTextViewContext("Zs");
				break;
			// 现病史
			case R.id.composer_button_xbs:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("现病史");
				setTextViewContext("Xbs");
				break;
			// 既往史
			case R.id.composer_button_jws:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("既往史");
				setTextViewContext("Jws");
				break;
			// 个人史
			case R.id.composer_button_grs:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("个人史");
				setTextViewContext("Grs");
				break;
			// 婚姻史
			case R.id.composer_button_hys:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("婚姻史");
				setTextViewContext("Hys");
				break;
			// 家庭史
			case R.id.composer_button_jzs:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("家庭史");
				setTextViewContext("Jzs");
				break;
			// 辅助检查
			case R.id.composer_button_fzjc:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("辅助检查");
				setTextViewContext("Fzjc");
				break;
			// 系统回顾
			case R.id.composer_button_xthg:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("系统回顾");
				setTextViewContext("Xthg");
				break;
			// 月经史
			case R.id.composer_button_yjs:
				composerButtonsShowHideButton.performClick();
				tv_ryjltitle.setText("月经史");
				setTextViewContext("Yjs");
				break;
			case R.id.btn_title_back:
				PatientRyjl.this.finish();
				break;
			default:
				break;
			}

		}
	};

	/**
	 * 菜单UI
	 */
	private void initComposerUi() {

		composerButtonsWrapper = (RelativeLayout) findViewById(R.id.composer_buttons_wrapper);
		composerButtonsShowHideButton = (RelativeLayout) findViewById(R.id.composer_buttons_show_hide_button);
		composerButtonsShowHideButtonIcon = (ImageView) findViewById(R.id.composer_buttons_show_hide_button_icon);

		composerButtonsShowHideButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!areButtonsShowing) {
					MyAnimations.startAnimationsIn(composerButtonsWrapper, 300);
					composerButtonsShowHideButtonIcon
							.startAnimation(MyAnimations.getRotateAnimation(0,
									-270, 300));
				} else {
					MyAnimations
							.startAnimationsOut(composerButtonsWrapper, 300);
					composerButtonsShowHideButtonIcon
							.startAnimation(MyAnimations.getRotateAnimation(
									-270, 0, 300));
				}
				areButtonsShowing = !areButtonsShowing;
			}
		});
		for (int i = 0; i < composerButtonsWrapper.getChildCount(); i++) {
			composerButtonsWrapper.getChildAt(i).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
						}
					});
		}

		composerButtonsShowHideButton.startAnimation(MyAnimations
				.getRotateAnimation(0, 360, 200));
	}
}
